<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\builder\Builder;
use app\BaseController;
use think\facade\Db;
use think\Request;

class Myjur extends BaseController
{
    public function show_list(){
        return Builder::table()
            ->setAction('myjur/page?model_id=8')
            ->setMonitorAction('switch','myjur/change')
            ->setRightHeader()
            ->addToolbarDemo('alldel','全部删除',[],'myjur/del')
            ->addToolbarDemo('add','添加',[],'myjur/show_add')
            ->setButton('detail','详情','myjur/detail')
            ->setButton('edit','编辑','myjur/show_edit')
            ->setButton('del','删除','myjur/del')
            ->setColumn('input','name','权限名','myjur/change')
            ->setColumn('input','model_id','权限所在表','myjur/change')
            ->setColumn('input','cid','权限所在栏目','myjur/change')
            ->setColumn('input','descs','权限描述','myjur/change')
            ->setColumn('switch','status','状态','myjur/change')
            ->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param('limit');
        $data = Db::name(getTable('jurs'))
            ->order('id desc')
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            if($value['cid']){
                $cid = Db::name(getTable('column'))
                    ->where('id',$value['cid'])
                    ->find();
                if($cid){
                    $value['cid'] = $cid['name'];
                }else{
                    $value['cid'] = '';
                }
            }else{
                $value['cid'] = '';
            }
            if($value['model_id']){
                $model = Db::name(getTable('models'))
                    ->where('id',$value['model_id'])
                    ->find();
                if($model){
                    $value['model_id'] = $model['name'];
                }else{
                    $value['model_id'] = '';
                }
            }else{
                $value['model_id'] = '';
            }
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }
    public function show_add(Request $request){
        $cid = Db::name(getTable('column'))
            ->where('status',1)
            ->select()->toArray();

        return Builder::form()
            ->setAction('myjur/add')
            ->setProject('input','name','权限名称')
            ->setProject('select','cid','选择栏目','',$cid,'',true)
            ->setProject('switch','status','状态')
            ->setButton('submit')
            ->setButton('back')
            ->fetch();
    }
    public function add(Request $request){
        $data = $request->param();
        $newdata['cid'] = $data['cid'];
        if($data['status']??null){
            $newdata['status'] = 1;
        }else{
            $newdata['status'] = 0;
        }
        $newdata['name'] = $data['name'];
        $array = ['增','删','改','查'];
        foreach ($array as $value){
            $newdata['descs'] = $value;
            $newdata['create_time'] = time();
            Db::name(getTable('jurs'))->insert($newdata);
        }
        return $this->success('添加成功');
    }
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('jurs'))
            ->where('id',$id)
            ->find();
        $cid = Db::name(getTable('column'))
            ->where('status',1)
            ->select()->toArray();
        return Builder::form()
            ->setAction('myjur/edit')
            ->setProject('none','id','',$id)
            ->setProject('input','name','权限名称',$data['name'])
            ->setProject('select','cid','选择栏目',$data['cid'],$cid,'',true)
            ->setProject('switch','status','状态',$data['status'])
            ->setButton('submit')
            ->setButton('back')
            ->fetch();
    }
    public function change(Request $request){
        $data = $request->param();
        $shuju = Db::name(getTable('jurs'))
            ->where('id',$data['id'])
            ->find();
        if($shuju[$data['field']]==1){
            $shuju[$data['field']] = 0;
        }else{
            $shuju[$data['field']] = 1;
        }
        if(Db::name(getTable('jurs'))->where('id',$shuju['id'])->update($shuju)){
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }
    public function detail(Request $request){
        $id = $request->param('id');
        $data = Db::name(getTable('jurs'))
            ->where('id',$id)
            ->find();
        $cid = Db::name(getTable('column'))
            ->where('status',1)
            ->select()->toArray();
        return Builder::form()
            ->setAction('myjur/edit')
            ->setProject('none','id','',$id)
            ->setProject('input','name','权限名称',$data['name'])
            ->setProject('select','cid','选择栏目',$data['cid'],$cid,'',true)
            ->setProject('switch','status','状态',$data['status'])
//            ->setButton('submit')
            ->setButton('back')
            ->fetch();
    }
    public function edit(Request $request){
        $data = $request->param();
        $newdata['name'] = $data['name'];
        $newdata['cid'] = $data['cid'];
        if($data['status']??null){
            $newdata['status'] = 1;
        }else{
            $newdata['status'] = 0;
        }
        $jiu = Db::name(getTable('jurs'))
            ->where('id',$data['id'])
            ->find();
        $jius = Db::name(getTable('jurs'))
            ->where('name',$jiu['name'])
            ->select()->toArray();
        foreach ($jius as $value){
            $value['name'] = $newdata['name'];
            $value['cid'] = $newdata['cid'];
            $value['status'] = $newdata['status'];
            Db::name(getTable('jurs'))
                ->where('id',$value['id'])
                ->update($value);
        }
        return $this->success('修改成功');
    }
    public function del(Request $request){
        $id = $request->param('id');
        $user = session('adminuser');
        if($user['role_id']!==1){
            return json_to_respos('你没有权限删除');
        }
        if(Db::name(getTable('jurs'))->where('id','in',$id)->delete()){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}
